package com.xyw.his00.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@PropertySource(value="classpath:redisConfig.properties")
public class JedisConfig {

    @Value(value = "${redis.host}")
    private String host;//主机地址
    @Value(value = "${redis.port}")
    private int port;//端口号
    @Value(value = "${redis.password}")
    private String password;//密码
    @Value(value = "${redis.database}")
    private int database;//数据库
    @Value(value = "${redis.maxTotal}")
    private int maxTotal;//最大连接数
    @Value(value = "${redis.maxIdle}")
    private int maxIdle;//最大空闲连接数
    @Value(value = "${redis.minIdle}")
    private int minIdle;//最小空闲连接数
    @Value(value = "${redis.maxWaitMillis}")
    private int maxWaitMillis;//最大等待时间毫秒
    @Value(value = "${redis.timeout}")
    private int timeout;//超时时间毫秒
    @Value(value = "${redis.testOnBorrow}")
    private boolean testOnBorrow;//从连接池获取连接时是否测试连接可用性

    /**
     * spring容器创建一个内存中实例，名字和name值相同
     */

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setTestOnBorrow(testOnBorrow);
        return jedisPoolConfig;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory(jedisPoolConfig());
        factory.setHostName(host);
        factory.setPort(port);
        factory.setPassword(password);
        factory.setDatabase(database);
        factory.setTimeout(timeout);
        factory.afterPropertiesSet();
        return factory;
    }
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

}
